package com.iemes.controller.equip;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.iemes.annotation.SystemLog;
import com.iemes.controller.index.BaseController;
import com.iemes.entity.EquipSparepartFormMap;
import com.iemes.entity.ResFormMap;
import com.iemes.entity.UserFormMap;
import com.iemes.mapper.equip.EquipSparepartMapper;
import com.iemes.plugin.PageView;
import com.iemes.util.Common;

@RequestMapping("equip")
@Controller
public class EquipSparepartController extends BaseController{
	@Inject
	private EquipSparepartMapper sparepartMapper;
	
	@RequestMapping(value = "equip_sparepart", method = RequestMethod.GET, produces = "text/html; charset=utf-8")
	public String examinepru(Model model) {
		List<ResFormMap> findByRes = findByRes();
		//model.addAttribute("categoryFormMap", categoryFormMap);
		model.addAttribute("res", findByRes);
		return Common.BACKGROUND_PATH + "/equip/sparepart/equipbom_list";				
	}
	@ResponseBody
	@RequestMapping(value ="equip_sparepartByPage")
	public PageView findByPage(String pageNow, String pageSize) {
		EquipSparepartFormMap equipSparepartFormMap = getFormMap(EquipSparepartFormMap.class);
		if (equipSparepartFormMap==null) {
			equipSparepartFormMap.put("status", 1);
		}
		List<Map<String,Object>> sparepartSearch = sparepartMapper.getSparepartSearch(equipSparepartFormMap);
		try {
			LuogicPaging(pageNow, pageSize, sparepartSearch);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
			return pageView;	
	}

	
	@RequestMapping(value ="spareparts_edit")
	public String editUI(Model model) throws Exception {
		String id = getPara("id");
		EquipSparepartFormMap findbyFrist = sparepartMapper.findbyFrist("id", id,
				EquipSparepartFormMap.class);
		model.addAttribute("equipAll", findbyFrist);
		return Common.BACKGROUND_PATH + "/equip/sparepart/equipbom_edit";
	}

	@ResponseBody
	@RequestMapping(value = "sparepart_editEntity")
	@SystemLog(module = "设备配件信息表", methods = "设备-修改配件信息表")
	// 凡需要处理业务逻辑的.都需要记录操作日志
	public String editEntity() throws Exception {
		EquipSparepartFormMap equipSparepartFormMap = getFormMap(EquipSparepartFormMap.class);
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
		// 通过工具类获取当前登录的bean supervisorId 
		UserFormMap userFormMap = (UserFormMap) Common.findUserSession(request);
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date=new Date();
		equipSparepartFormMap.put("date", sdf.format(date));
		equipSparepartFormMap.put("fillPeopleId", userFormMap.get("id")); 
		sparepartMapper.editEntity(equipSparepartFormMap);
		return "success";
	}
	
	@ResponseBody
	@RequestMapping(value = "sparepart_deleteEntity")
	@SystemLog(module = "设备配件信息表", methods = "设备-删除配件信息表")
	// 凡需要处理业务逻辑的.都需要记录操作日志
	public String deleteEntity() throws Exception {
		EquipSparepartFormMap equipSparepartFormMap = getFormMap(EquipSparepartFormMap.class);
		String[] ids = getParaValues("ids");
		String arr = ids[0];
		String[] str = arr.split(",");HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
		// 通过工具类获取当前登录的bean supervisorId 
		UserFormMap userFormMap = (UserFormMap) Common.findUserSession(request);
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date=new Date();
		for (int i = 0; i < str.length; i++) {
			equipSparepartFormMap.set("status", 2);
			equipSparepartFormMap.put("id", str[i]);
			equipSparepartFormMap.put("date", sdf.format(date));
			equipSparepartFormMap.put("fillPeopleId", userFormMap.get("id")); 
			sparepartMapper.editEntity(equipSparepartFormMap);
		}
		return "success";
	}
	
}
